package huixue.cn.com.rxapp.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import huixue.cn.com.rxapp.R;

/**
 * Created by gongdongyang 2019/7/14
 * Describe:
 */
public class ReflectView extends View {

    private Bitmap mSrcBmp;
    private Bitmap mRefBmp;

    private Paint mPaint;

    public ReflectView(Context context) {
        this(context,null);
    }

    public ReflectView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ReflectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        mSrcBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.timg);

        Matrix matrix = new Matrix();
        matrix.setScale(1,-1);


        mRefBmp = Bitmap.createBitmap(mSrcBmp,0,0,
                mSrcBmp.getWidth(),mSrcBmp.getHeight(),matrix,false);

        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);

        LinearGradient linearGradient =
                new LinearGradient(0,mSrcBmp.getHeight(),0,
                mSrcBmp.getHeight()*1.4f,
                        0xDD000000,0x10000000, Shader.TileMode.CLAMP);

        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setShader(linearGradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mSrcBmp,0,0,null);
        canvas.drawBitmap(mRefBmp,0,mSrcBmp.getHeight(),null);

        canvas.drawRect(new Rect(0,mSrcBmp.getHeight(),mSrcBmp.getWidth(),mSrcBmp.getHeight()*2),mPaint);
    }
}
